package com.mars.service.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mars.common.exception.BizException;
import com.mars.common.page.PageParam;
import com.mars.common.page.constants.PageConst;
import com.mars.common.utils.CommonUtils;
import com.mars.mybatis.support.BaseMapper;

/**
 * 基础Service<br>
 * Service实现类可以继承该基础类，该类封装了CRUD操作<br>
 * 该Service基础类不是必须的
 * 
 * @param <T>
 *            Mapper对应的实体对象
 * @param <PK>
 *            主键的类型
 * @author 杨浩泉
 * @version 2016年10月14日
 */
public class BaseService<T, PK> implements IService<T, PK> {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BaseMapper<T, PK> baseMapper;

	@Override
	public int insert(T entity) throws BizException {
		try {
			return this.baseMapper.insert(entity);
		} catch (Exception e) {
			throw BizException.DB_INSERT_RESULT_0.logError(e);
		}
	}

	@Override
	public int insertSelective(T entity) throws BizException {
		try {
			return this.baseMapper.insertSelective(entity);
		} catch (Exception e) {
			throw BizException.DB_INSERT_RESULT_0.logError(e);
		}
	}

	@Override
	public int insertBatch(List<T> entityList) throws BizException {
		try {
			return this.baseMapper.insertList(entityList);
		} catch (Exception e) {
			throw BizException.DB_BATCH_INSERT_RESULT_ERROR.logError(e);
		}
	}

	@Override
	public int inserOrUpdate(T entity) throws BizException {
		try {
			return this.baseMapper.inserOrUpdate(entity);
		} catch (Exception e) {
			throw BizException.DB_INSERT_OR_UPDATE_RESULT_ERROR.logError(e);
		}
	}

	@Override
	public int delete(PK id) throws BizException {
		try {
			return this.baseMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			throw BizException.DB_DELETE_RESULT_0.logError(e);
		}
	}

	@Override
	public int deleteSelective(T entity) throws BizException {
		try {
			return this.baseMapper.delete(entity);
		} catch (Exception e) {
			throw BizException.DB_DELETE_RESULT_0.logError(e);
		}
	}
	
	@Override
	public int deleteByIds(List<PK> idList) throws BizException {
		try {
			return this.baseMapper.deleteBatchIds(idList);
		} catch (Exception e) {
			throw BizException.DB_DELETE_RESULT_0.logError(e);
		}
	}

	@Override
	public int deleteByIds(String ids) throws BizException {
		try {
			return this.baseMapper.deleteByIds(ids) ;
		} catch (Exception e) {
			throw BizException.DB_DELETE_RESULT_0.logError(e);
		}
	}

	@Override
	public int update(T entity) throws BizException {
		try {
			return this.baseMapper.updateByPrimaryKey(entity);
		} catch (Exception e) {
			throw BizException.DB_UPDATE_RESULT_0.logError(e);
		}
	}

	@Override
	public int updateSelective(T entity) throws BizException {
		try {
			return this.baseMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			throw BizException.DB_UPDATE_RESULT_0.logError(e);
		}
	}

	@Override
	public int update(T entity, T whereEntity) throws BizException {
		try {
			return this.baseMapper.update(entity, whereEntity);
		} catch (Exception e) {
			throw BizException.DB_UPDATE_RESULT_0.logError(e);
		}
	}

	@Override
	public int updateSelective(T entity, T whereEntity) throws BizException {
		try {
			return this.baseMapper.updateSelective(entity, whereEntity);
		} catch (Exception e) {
			throw BizException.DB_UPDATE_RESULT_0.logError(e);
		}
	}

	@Override
	public int updateBatchById(List<T> entityList) throws BizException {
		try {
			return this.baseMapper.updateBatchById(entityList);
		} catch (Exception e) {
			throw BizException.DB_UPDATE_RESULT_0.logError(e);
		}
	}

	@Override
	public T selectById(PK id) throws BizException {
		try {
			return this.baseMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			throw BizException.DB_SELECTONE_IS_NULL.logError(e);
		}
	}
	
	@Override
	public List<T> selectByIds(List<PK> idList) throws BizException {
		try {
			return this.baseMapper.selectBatchIds(idList);
		} catch (Exception e) {
			throw BizException.DB_LIST_IS_NULL.logError(e);
		}
	}

	@Override
	public List<T> selectByIds(String ids) throws BizException {
		try {
			return this.baseMapper.selectByIds(ids) ;
		} catch (Exception e) {
			throw BizException.DB_LIST_IS_NULL.logError(e);
		}
	}

	@Override
	public T selectOne(T entity) throws BizException {
		try {
			return this.baseMapper.selectOne(entity);
		} catch (Exception e) {
			throw new BizException("数据库操作,查询对象异常：%s", e.getMessage()).logError();
		}
	}

	@Override
	public int selectCount(T entity) throws BizException {
		try {
			return this.baseMapper.selectCount(entity);
		} catch (Exception e) {
			throw BizException.DB_COUNT_ERROR.logError(e);
		}
	}

	@Override
	public List<T> selectList(T entity, String orderBy) throws BizException {
		try {
			return this.baseMapper.selectList(entity, orderBy);
		} catch (Exception e) {
			throw BizException.DB_LIST_IS_NULL.logError(e);
		}
	}

	@Override
	public List<T> selectList(T entity) throws BizException {
		try {
			return this.baseMapper.select(entity);
		} catch (Exception e) {
			throw BizException.DB_LIST_IS_NULL.logError(e);
		}
	}

	@Override
	public PageInfo<T> selectPage(PageParam pagepram, T entity, String orderBy) throws BizException {
		try {
			Page<T> page = PageHelper.startPage(pagepram.getPage(), pagepram.getRows(), orderBy);
			this.baseMapper.selectPage(entity);
			return new PageInfo<T>(page);
		} catch (Exception e) {
			throw new BizException("数据库操作,分页查询异常：%s", e.getMessage()).logError();
		}
	}

	@Override
	public PageInfo<T> selectPage(PageParam pagepram, T entity) throws BizException {
		return selectPage(pagepram, entity, null);
	}

	@Override
	public PageInfo<T> selectPageByMap(Map<String, Object> params) throws BizException {
		try {
			Map<String, Object> pageMap = new HashMap<String, Object>() ;
			if (CommonUtils.isEmpty(params.get(PageConst.PAGE_NUM))) {
				pageMap.put(PageConst.PAGE_NUM, PageConst.DEFAULT_PAGE);
			}
			if (CommonUtils.isEmpty(params.get(PageConst.PAGE_SIZE))) {
				pageMap.put(PageConst.PAGE_SIZE, PageConst.DEFAULT_ROWS);
			}
			if (CommonUtils.isEmpty(params.get(PageConst.ORDER_BY))) {
				pageMap.put(PageConst.ORDER_BY, PageConst.DEFAULT_ORDER_FIELD);
			}
			
			Page<T> page = PageHelper.startPage(pageMap) ;
			baseMapper.selectPageByMap(params) ;
			return new PageInfo<T>(page) ;
		} catch (Exception e) {
			throw BizException.DB_SELECT_PAGE.logError(e);
		}
	}

	@Override
	public T checkValue(String field, Object value) throws BizException {
		try {
			return this.baseMapper.checkValue(field, value);
		} catch (Exception e) {
			throw BizException.DB_CHECK_VALUE.logError(e);
		}
	}

}
